package be.phl.obstacleapp;

import java.util.HashSet;
import java.util.Set;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import be.phl.model.DBAdapterScreening;

public class ScreeningProblemSummaryFragment extends Fragment {

	private int ScreeningRoomId;
	public static final String ARG_SCREENINGROOM_ID = "ScreeningRoomId";
	Set<itemProblem> itemProblems = new HashSet<itemProblem>();

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(
				R.layout.fragment_screening_problem_summary, container, false);

		ScreeningRoomId = getArguments().getInt(ARG_SCREENINGROOM_ID);

		// get all the screeningproblems and add them to the rootlayout
		DBAdapterScreening db = new DBAdapterScreening(getActivity());
		db.open();
		Cursor cp = db.getItemProblemsRoom(ScreeningRoomId);
		while (cp.moveToNext()) {
			itemProblem ip = new itemProblem();
			ip.setId(cp.getInt(0));
			ip.setParentId(cp.getInt(2));
			ip.setName(cp.getString(3));
			itemProblems.add(ip);
		}

		Cursor c = db.getAllScreeningProblems(ScreeningRoomId);
		while (c.moveToNext()) {
			TextView label = new TextView(getActivity());
			label.setTextSize(18);
			label.setText(c.getString(9) + ": " + SetLabel(c.getInt(2))
					+ c.getString(3));
			((LinearLayout) rootView).addView(label);
		}
		db.close();

		return rootView;

	}

	/**
	 * add the names of the parents
	 * 
	 * @param parentId
	 * @return
	 */
	private String SetLabel(int parentId) {

		for (itemProblem ip : itemProblems) {
			if (ip.getId() == parentId && ip.getId() >= 0) {
				return SetLabel(ip.getParentId()) + ip.getName() + " => ";
			}
		}

		return "";
	}

	static class itemProblem {
		private int id, parentId;
		private String name;

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public int getParentId() {
			return parentId;
		}

		public void setParentId(int parentId) {
			this.parentId = parentId;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

	}

}
